<template>
  <static-content-wrapper
    :designer="designer"
    :settings="settings"
    :field="field"
    :design-state="designState"
    :hidden="field.options.hidden"
    :parent-widget="parentWidget"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :sub-form-row-id="subFormRowId"
    :allow-select="allowSelect"
  >
    <div
      ref="fieldEditor"
      class="field-editor module-field-editor"
      :style="{
        display: field.widgetList && field.widgetList.length > 0 ? 'block' : 'flex',
        justifyContent: justifyContent,
        alignItems: alignItems,
        width: scaleWidth + 'px',
        height: scaleHeight + 'px',
        backgroundColor: backgroundColor,
        borderStyle: 'solid',
        borderWidth: scaleBorderWidth + 'px',
        borderColor: field.options.colorLine,
        borderRadius: scaleBorderRadius + 'px',
        opacity: field.options.opa / 255
      }"
    ></div>
    <template v-for="(widget, index) in field.widgetList || []" :key="widget.id">
      <div class="widget-list-container module-container">
        <component
          :is="getWidgetName(widget)"
          :field="widget"
          :designer="designer"
          :parent-list="field.widgetList"
          :index-of-parent-list="index"
          :parent-widget="field"
          :design-state="true"
          :allow-select="false"
          :scale="scale"
          :style="{
            left: widget.options.x * scale + 'px',
            top: widget.options.y * scale + 'px'
          }"
        ></component>
      </div>
    </template>
  </static-content-wrapper>
</template>

<script>
import emitter from '@/mixins/emitter'
import fieldMixin from '@/mixins/fieldMixin'
import { ALIGN_TYPE } from '@/designer/const'

export default {
  name: 'ModuleWidget',
  componentName: 'ModuleWidget',
  // 必须固定为FieldWidget，用于接收父级组件的broadcast事件
  mixins: [emitter, fieldMixin],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    settings: Object,
    scale: {
      type: Number,
      default: 1
    },
    allowSelect: {
      type: Boolean,
      default: true
    },

    designState: {
      type: Boolean,
      default: false
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */
      type: Number,
      default: -1
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */
      type: Number,
      default: -1
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */
      type: String,
      default: ''
    }
  },
  data() {
    return {}
  },
  computed: {
    scaleWidth() {
      return this.field.options.width * this.scale
    },
    scaleHeight() {
      return this.field.options.height * this.scale
    },
    scaleBorderWidth() {
      return this.field.options.widthLine * this.scale
    },
    scaleBorderRadius() {
      return this.field.options.radius * this.scale
    },
    justifyContent() {
      const align = this.field.options.titleAlign
      if (
        align === ALIGN_TYPE.LV_ALIGN_TOP_LEFT ||
        align === ALIGN_TYPE.LV_ALIGN_LEFT_MID ||
        align === ALIGN_TYPE.LV_ALIGN_BOTTOM_LEFT
      ) {
        return 'start'
      } else if (
        align === ALIGN_TYPE.LV_ALIGN_TOP_RIGHT ||
        align === ALIGN_TYPE.LV_ALIGN_RIGHT_MID ||
        align === ALIGN_TYPE.LV_ALIGN_BOTTOM_RIGHT
      ) {
        return 'end'
      } else {
        return 'center'
      }
    },
    alignItems() {
      const align = this.field.options.titleAlign
      if (
        align === ALIGN_TYPE.LV_ALIGN_TOP_LEFT ||
        align === ALIGN_TYPE.LV_ALIGN_TOP_MID ||
        align === ALIGN_TYPE.LV_ALIGN_TOP_RIGHT
      ) {
        return 'start'
      } else if (
        align === ALIGN_TYPE.LV_ALIGN_BOTTOM_LEFT ||
        align === ALIGN_TYPE.LV_ALIGN_BOTTOM_MID ||
        align === ALIGN_TYPE.LV_ALIGN_BOTTOM_RIGHT
      ) {
        return 'end'
      } else {
        return 'center'
      }
    },
    backgroundPositionX() {
      const align = this.field.options.pngAlign
      if (
        align === ALIGN_TYPE.LV_ALIGN_TOP_LEFT ||
        align === ALIGN_TYPE.LV_ALIGN_LEFT_MID ||
        align === ALIGN_TYPE.LV_ALIGN_BOTTOM_LEFT
      ) {
        return 'left'
      } else if (
        align === ALIGN_TYPE.LV_ALIGN_TOP_RIGHT ||
        align === ALIGN_TYPE.LV_ALIGN_RIGHT_MID ||
        align === ALIGN_TYPE.LV_ALIGN_BOTTOM_RIGHT
      ) {
        return 'right'
      } else {
        return 'center'
      }
    },
    backgroundColor() {
      return this.field.options.color
    }
  },
  watch: {},
  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
         需要在父组件created中初始化！！ */
    this.registerToRefList()
    this.initEventHandler()

    this.handleOnCreated()
  },

  mounted() {
    this.handleOnMounted()
  },

  beforeUnmount() {
    this.unregisterFromRefList()
  },

  methods: {
    getWidgetName(widget) {
      return widget.type + '-widget'
    }
  }
}
</script>

<style lang="scss" scoped>
.button-field-editor {
  overflow: hidden;

  .module-field-editor-item-wrapper {
    position: absolute;
  }
}
</style>
